#include "TaskTrafficLight.hpp"
#include "../../Util/BridgeIOConfig.hpp"
#include "cybertron/glm/quat.hpp"
CTaskTrafficLight::CTaskTrafficLight() {
	mCommandID = Bridge::EHotAreaTrafficLightData;
}
CTaskTrafficLight::~CTaskTrafficLight() {

}
void CTaskTrafficLight::HotAreaEnter() {
	mTrafficLightsMap.clear();
}
void CTaskTrafficLight::HotAreaLeft(int frame, int ms, int msModNs, TrafficActorHotArea* pHotArea) {
	
}
void CTaskTrafficLight::ActorToDo(int hostVehicleId, int frame, int ms, int msModNs, TrafficActorHotArea* pHotArea, TrafficActor* pActor, int index) {

	SimOne_Data_TrafficLights data_trafficlights;
	auto lastBridgeLightInfo = mTrafficLightsMap.find(std::to_string(hostVehicleId));
	int size = 0;
	if (lastBridgeLightInfo != mTrafficLightsMap.end())
	{
		data_trafficlights = lastBridgeLightInfo->second;
		size = data_trafficlights.trafficlightNum;
		if (size >= SOSM_TRAFFICLIGHT_SIZE_MAX)
		{
			return;
		}
	}
	
	HotArea::TrafficLightPrivateData light;
	pActor->getCustomProperties(light);

	::google::protobuf::int32 opendriveLightId = light.opendrivelightid();
	::HotArea::ESignalStateType type = light.signalstatetype();
	auto isHostVehicleNextTL = light.ismainvehiclenexttrafficlight();
	auto tLBindHostVehicleId = light.mainvehicleactorid();
	if (isHostVehicleNextTL && tLBindHostVehicleId != hostVehicleId)
	{
		return;
	}
	if (type == ::HotArea::ESignalStateType::ESignalStateOff)
	{
		return;
	}
	if (opendriveLightId == -1 && index != -1)
	{
		return;
	}

	data_trafficlights.trafficlights[size].isMainVehicleNextTrafficLight = isHostVehicleNextTL;
	data_trafficlights.trafficlights[size].opendriveLightId = opendriveLightId;
	if (type == ::HotArea::ESignalStateType::ESignalStateGreen)
	{
		data_trafficlights.trafficlights[size].status = ESimOne_TrafficLight_Status::ESimOne_TrafficLight_Status_Green;
	}
	else if (type == ::HotArea::ESignalStateType::ESignalStateAmber)
	{
		data_trafficlights.trafficlights[size].status = ESimOne_TrafficLight_Status::ESimOne_TrafficLight_Status_Yellow;
	}
	else if (type == ::HotArea::ESignalStateType::ESignalStateRed)
	{
		data_trafficlights.trafficlights[size].status = ESimOne_TrafficLight_Status::ESimOne_TrafficLight_Status_Red;
	}

	data_trafficlights.trafficlights[size].countDown = light.countdown();
	data_trafficlights.trafficlightNum = size + 1;
	mTrafficLightsMap[std::to_string(hostVehicleId)] = data_trafficlights;
}

char* CTaskTrafficLight::GetResult(std::string hostVehicleId) {
	for (auto item = mTrafficLightsMap.begin(); item != mTrafficLightsMap.end(); item++) {
		if (item->first == hostVehicleId) {
			return (char*)&item->second;
		}
	}
	return nullptr;
	//return (char*)&mLastBridgeLightInfo;
}

int CTaskTrafficLight::GetHotAreaDataLength(std::string hostVehicleId) {
	return sizeof(SimOne_Data_TrafficLights);
}

void CTaskTrafficLight::Clear() {
	mResultMessage.clear();
}
